requireJS(['constant', 'site', 'handlebars'], function (Constant, site, Handlebars) {
    var url = {
        list: "manager/selectDepartment.do",
        addDepartment: "manager/addDepartment.do",
        editDepartment: "manager/updateDepartment.do",
        deleteDepartment: "manager/delDepartment.do"
    }

    //查询产品所需字段
    var frontData = {
        pageNum: 1
    };
    var html = [];

    var id, $this;

    //全部权限
    var departments = {};
    //查询产品
    var queryData = function () {
        site.send({
            url: url.list,
            //async:false
            data: frontData,
            callback: function (data) {
                var data = JSON.parse(data);
                htmlbtn(data.content);
                $('#htmlBody').html(html.join(''));

                $('#htmlBody').treeview({
                    collapsed: true,
                    animated: 'medium',
                    control: '#sidetreecontrol',
                    persist: 'cookie'
                });
                initBtnAction();
            }
        })
    }

    var htmlbtn = function (content) {
        html.push('<ul>');
        $.each(content, function (i, t) {
            departments[t.id] = t;


            html.push('<li>');
            if (t.children.length != 0) {
                html.push('<div class="tr1" data-type ="true" data-has-child="true" data-focus="false" data-toggle="collapse" data-target=".tr11" >');
                // html.push('<td>[-]</td>');
            } else {
                html.push('<div class="tr1" data-type ="true" data-has-child="false" data-focus="false">');
                // html.push('<td></td>');
            }

            html.push('<label>' + t.name + '</label>');
            html.push('<label>');
            html.push('<span ng-if="HbHr.hr210" class="_yellow" data-attr="addDepartment" data-type="2" data-name="' + t.name + '" data-id="' + t.id + '">');
            html.push('添加子部门');
            html.push('</span>');
            html.push('<span ng-if="HbHr.hr211" class="second-span _blue" data-attr="editDepartment" data-id="' + t.id + '" data-name="' + t.name + '" data-parentId="' + t.parentId + '">');
            html.push('编辑部门');
            html.push('</span>');
            html.push('<span ng-if="HbHr.hr212" class="second-span _red" data-attr="deleteDepartment" data-name="' + t.name + '"data-id="' + t.id + '">删除部门</span>');
            html.push('</lable>');
            html.push('</div>');
            htmlbtn1(t.children, 'tr11');

            html.push('</li>');
        });

        html.push('</li>');
    }
    var htmlbtn1 = function (content, ii) {
        html.push('<ul>');
        $.each(content, function (i, t) {
            html.push('<li>');
            departments[t.id] = t;

            if (t.children.length != 0) {
                html.push('<div class="' + ii + ' panel-collapse collapse in" data-type ="true" data-has-child="true" data-focus="false" >');
                // html.push('<td>[-]</td>');
            } else {
                html.push('<div class="' + ii + ' panel-collapse collapse in" data-type ="true" data-has-child="false" data-focus="false">');
                // html.push('<td></td>');
            }
            html.push('<label>');
            html.push('<span class="toggle-tu">');
            html.push('<span class="tu-1"></span>');
            html.push('<span class="tu-2"></span>');
            html.push('</span>');
            html.push('</lable>');
            html.push('</label>');
            html.push(t.name);


            html.push('<label>');
            if (ii != 'tr1111') {
                html.push('<span ng-if="HbHr.hr210" class="_yellow" data-attr="addDepartment" data-type="2" data-name="' + t.name + '" data-id="' + t.id + '">');
                html.push('添加子部门');
                html.push('</span>');
            }
            html.push('<span ng-if="HbHr.hr211" class="second-span _blue" data-attr="editDepartment" data-id="' + t.id + '" data-name="' + t.name + '" data-parentId="' + t.parentId + '">');
            html.push('编辑部门');
            html.push('</span>');
            html.push('<span ng-if="HbHr.hr212" class="second-span _red" data-attr="deleteDepartment" data-id="' + t.id + '" data-name="' + t.name + '">删除部门</span>');
            html.push('</lable>');
            html.push('</div>');
            if (t.children.length != 0) {
                htmlbtn1(t.children, ii + 1);
            }
            html.push('</li>');
        });
        html.push('</ul>');
    };
    //data-attr="addSection"
    //点击添加菜单按钮将内容填充到模板
    $('#addTop').on('click', function () {
        var source = $("#department_tpl").html();
        var template = Handlebars.compile(source);
        var r = {parentId: 0, pName: "顶级"}
        $('#department').html(template(r));
        $('.addwinBox').toggle();
        $("#image_upload_pop").hide()
        initWinBoxAciton("addDepartment");

        // var r = rights[id];
        // r.pId = id;
        // r.pName = name;
    });
    //初始化绑定按钮
    var initWinBoxAciton = function (attr) {

        //点击提交按钮
        $("#submit").on('click', function () {
            if (attr == "addDepartment") {
                addDepartment(url.addDepartment);
            }
            if (attr == "editDepartment") {
                editDepartment(url.editDepartment);
            }
        })

        //添加部门和添加子部门
        function addDepartment(url) {
            var data = {
                dname: $('#dname').val(),
                parentId: $('#parentId').val()
            };
            site.send({
                url: url,
                data: data,
                callback: function (data) {
                    data = JSON.parse(data)
                    if (data.type == 1) {
                        $('.winBox').hide();
                        $("#image_upload_pop").hide();
                        Constant.fn.Message('T', data.msg);
                        // pjaxReload();//刷新当前页面.
                        html = [];
                        queryData();

                    } else {
                        Constant.fn.Message('F', data.msg);
                        $("#image_upload_pop").hide()
                    }
                }
            })
        }

        function editDepartment(url) {
            //如果是编辑的话，获取弹窗里面的参数
            var data = {
                dname: $('#dname').val(),
                parentId: $('#parentId').val(),
                id: $('#id').val()
            };
            site.send({
                url: url,
                data: data,
                callback: function (data) {
                    data = JSON.parse(data)
                    if (data.type == 1) {
                        Constant.fn.Message('T', data.msg);
                        $('.winBox').hide();
                        $("#image_upload_pop").hide();
                        html = [];
                        queryData();
                        // pjaxReload();//刷新当前页面.
                    } else {
                        // $('.winBox').toggle();
                        Constant.fn.Message('F', data.msg);
                        $("#image_upload_pop").hide()
                    }
                }
            })
        }
    }

    var initBtnAction = function () {
        //添加子部门
        $('span[data-attr=addDepartment]').on('click', function () {
            var id = $(this).data('id');
            var name = $(this).data('name');
            var r = {parentId: id, pName: name}
            var source = $("#department_tpl").html();
            var template = Handlebars.compile(source);
            $('#department').html(template(r));
            $('.addwinBox').toggle();
            $("#image_upload_pop").hide()
            initWinBoxAciton($(this).data("attr"));
        });
        //删除部门
        $('span[data-attr=deleteDepartment]').on('click', function () {
            var name = $(this).data('name')
            $this = $(this);
            id = $(this).data('id');
            $('.delete_winBox').show();
        })

        $("#sureDelete").unbind().on('click', function () {
            site.send({
                url: url.deleteDepartment,
                data: {
                    id: id
                },
                callback: function (data) {
                    data = JSON.parse(data)
                    if (data.type == 1) {
                        Constant.fn.Message('T', data.msg);                            // $('.winBox').toggle();
                        $('.delete_winBox').hide();
                        $("#image_upload_pop").hide()
                        initWinBoxAciton($(this).data('attr'));
                        // pjaxReload();//刷新当前页面.
                        $this.parent('label').parent('div').parent('li').remove();
                    }
                    if (data.type == -1) {
                        initWinBoxAciton($(this).data('attr'));
                        Constant.fn.Message('F', data.msg);                            // $('.winBox').toggle();
                        $("#image_upload_pop").hide()
                    }
                }
            })
        })

        //编辑部门
        $('span[data-attr=editDepartment]').on('click', function () {
            //获取当前点击对象的id
            var id = $(this).data('id');
            var name = $(this).data('name');
            //根据当前id获取当前权限对象
            var r = departments[id];
            if (r.parentId == 0) {
                r.pName = "顶级菜单";
            } else {
                r.pName = departments[r.parentId].name;
            }
            var source = $("#department_tpl").html();
            var template = Handlebars.compile(source);
            $('#department').html(template(r));
            $(".headerSpan").text("编辑部门");
            $('.addwinBox').toggle();
            $("#image_upload_pop").hide()
            initWinBoxAciton($(this).data('attr'));
        })
    }
    //进入时搜索
    queryData();

    //搜索
    $("#sousuo").on('click', function () {
        queryData();
    });


    //编辑
    $('#edit').on('click', function () {
        pjaxGo("product/product_edit.html")
    })

    $('#operations').on('change', function () {
        pjaxGo("process/process.html")
    })

    $("#operations_record").on('click', function () {
        $('.winBox').toggle().load('loanBefore/common/record.html');
    })


    // $(".treeview ul li div").css("display", "inline-flex")
})

